package com.hqyj.web.servlet;

import com.alibaba.fastjson.JSONArray;
import com.hqyj.pojo.Book;
import com.hqyj.pojo.User;
import com.hqyj.service.UserService;
import com.hqyj.service.impl.UserServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author cz
 * 2021/8/11 19:40
 */
@WebServlet("/bookSystem.do")
public class BookSystemServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String flag = req.getParameter("flag");

        //业务操作
        if ("borrowed".equals(flag)) {
            this.findBooks(req, resp);
        } else if ("return".equals(flag)) {
            this.returnBook(req, resp);
        }else if ("unborrowed".equals(flag)){
            this.unborrowBooks(req,resp);
        }else if("borrow".equals(flag)){
            this.borrowBook(req,resp);
        }else if ("init".equals(flag)){
            this.findBookInit(req, resp);
        }
    }

    //借阅图书
    private void borrowBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        String bookId = req.getParameter("bookId");
        userService.modifyBookByBookId(Integer.parseInt(bookId),user.getId());
        //再次查询未借阅的图书
        List<Book> list = userService.findUnborrowBooks();
        JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
        PrintWriter pw = resp.getWriter();
        pw.println(jsonArray);
        resp.sendRedirect("/borrowBooks.jsp");
    }

    //查询未被借阅的图书
    private void unborrowBooks(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        List<Book> list = userService.findUnborrowBooks();
        JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
        PrintWriter pw = resp.getWriter();
        pw.println(jsonArray);
    }


    //归还图书
    private void returnBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("application/json;charset=utf-8");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        String bookId = req.getParameter("bookId");
        userService.modifyBookByBookId(Integer.parseInt(bookId));
        //再次查询用户已借阅的书
        List<Book> list = userService.findUserBooks(Integer.parseInt(bookId));
        JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
        PrintWriter pw = resp.getWriter();
        pw.println(jsonArray);
        resp.sendRedirect("/returnBooks.jsp");
    }

    //查询用户借阅的图书
    private void findBooks(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        List<Book> list = userService.findUserBooks(user.getId());
        JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
        PrintWriter pw = resp.getWriter();
        pw.println(jsonArray);
    }


    //访问主页面时，查询数据库中的值
    private void findBookInit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        List<Book> list = userService.findBookAll();
        //集合转换为json格式的数组
        JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
        PrintWriter pw = resp.getWriter();
        pw.println(jsonArray);
    }
}
